#include<stdio.h>
#define N 1000005
int prime[N], i, j, n, m, gap;
void fill() {
	prime[0] = prime[1] = 0;
	for (i = 2; i < N; i++)
		prime[i] = 1;
	for (i = 2; i * i <= N; i++)
		if (prime[i])
			for (j = 2 * i; j < N; j += i)
				prime[j] = 0;
}
int main() {
	fill();
	for (scanf("%d", &n); n--;) {
		int s = 0;
		scanf("%d", &m);
		for (gap = 0;; gap++) {
			if (m - gap >= 0 && prime[m - gap]) {
				printf("%d %d\n", m - gap, gap);
				break;
			} else if (m + gap <= N && prime[m + gap]) {
				printf("%d %d\n", m + gap, gap);
				break;
			}
		}

	}
	return 0;
}
